﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using LitJson;
public class FireData
{
   
    public string bulletName= "Bullet1";
    public int firerate = 1;
    public int firespeed = 20;
    public int Attack=10;
    public int[] firePoints;
    
}
public class Bullet : MonoBehaviour {
    public Transform[] FirePoints;
    public List<FireData> datas;
    float interval;
    float lastfireTime;
    GameObject bulletPrefab;
    FireData curData;
   public  int level = 1;
    public SpriteRenderer sprite;
    public Sprite Sprite1;
    public Sprite Sprite2;
    public Sprite Sprite3;
    // Use this for initialization
    void Start () {

        sprite =GetComponent<SpriteRenderer>();
        PoolManager.Instance.CreatPool(bulletPrefab);
	}
    private void Awake()
    {
        var json = Resources.Load<TextAsset>("data").text;
        datas = JsonMapper.ToObject<List<FireData>>(json);
        LoadDate();
    }
	
    void LoadDate()
    {
        
        curData = datas[level-1];
        interval = 1f / curData.firerate;
        bulletPrefab = Resources.Load<GameObject>("Bullet/"+curData.bulletName);
        
    }
	// Update is called once per frame
	void Update () {
        if (Time.time-lastfireTime>interval)
        {
            lastfireTime = Time.time;
            Fire();
        }
    }

    void Fire()
    {
        for (int i = 0; i < curData.firePoints.Length; i++)
        {
            int id = curData.firePoints[i];
            Transform point = FirePoints[id-1];
            var go = PoolManager.Instance.Get(bulletPrefab);
            go.transform.position = point.position;
            go.GetComponent<Rigidbody2D>().velocity = point.up * curData.firespeed;
            go.GetComponent<BulletFire>().attack=curData.Attack;
        }
        
    }
    public void LeveUp()
    {
        level++;
        if (level>1)
        {
            sprite.sprite = Sprite1;
        }
        if (level>2)
        {
            sprite.sprite = Sprite2;
        }
        if (level>3)
        {
            sprite.sprite = Sprite3;
            level = 4;
            
        }

        LoadDate();
        
    }
}
